const mysql = require('mysql');
const bill = require("./bill")
const user = require("./user")
let connection;
async function connectMysql() {
    connection = await mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: '111111',
        database: 'bill'
    })
    connection.connect((err) => {
        if (!err) {
            console.log('数据库连接成功');
            bill(sqlHandler)
            user(sqlHandler)
        }else{
            console.log("数据库连接失败",err);
        }
    })
}

function sqlHandler(sql) {
    if(!sql){
        return Promise.reject()
    }
    return new Promise((resolve, reject) => {
        connection.query(sql, (err, res) => {
            if (err) {
                reject(err);
            } else {
                resolve(res);
            }
        })
    })
}

module.exports = { connectMysql, sqlHandler }
